package com.ruoyi.redisDemo.srb;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/dictRedisController")
public class DictRedisController extends BaseController {
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    private static final String DICT_TYPE="srb:dict_type";

    private static final String DICT_DATA="srb:dict_type_data";

    /**
     * 测试默认使用存储方法实体 如何存储
     * 1 获取实体对象直接存储
     */
    @GetMapping("/saveDictType")
    public AjaxResult saveDictTypeRedis(){
        SysDictType sysDictType = sysDictTypeMapper.selectDictTypeById(1L);
        redisTemplate.opsForValue().set("srb:sysDictType:"+1,sysDictType,5, TimeUnit.MINUTES);
        return AjaxResult.success("ok");
    }
    @GetMapping("/getDictType")
    public AjaxResult getDictTypeRedis() {
        SysDictType sysDictType =(SysDictType) redisTemplate.opsForValue().get("srb:sysDictType:" + 1);
        return AjaxResult.success(sysDictType);
    }
    /**
     * 获取列表信息 如果redis中存在则直接返回，否则从数据库中获取，并存入到redis中
     */
    @GetMapping("/listDictType")
    @ApiOperation("获取字典类型列表")
    public TableDataInfo ListDictType() {
        startPage();
        List<SysDictType> listDictType=new ArrayList<>();

        try{
            //从redis中获取list列表
            listDictType =(List) redisTemplate.opsForValue().get(DICT_TYPE);
            if (listDictType != null) {
                return getDataTable(listDictType);
            }
        }catch (Exception e) {
            System.out.println("redis服务器异常");
        }

        System.out.println("-------从数据库中取值------");

        listDictType = sysDictTypeMapper.selectDictTypeList(new SysDictType());

        //将数据存入redis中
        try{
            redisTemplate.opsForValue().set(DICT_TYPE,listDictType,5,TimeUnit.MINUTES);
        }catch (Exception e) {
            System.out.println("redis服务异常");
        }

        return getDataTable(listDictType);
    }

    @GetMapping("/dict/type/list")
    public TableDataInfo listDict2(){
        startPage();
        List<SysDictType> sysDictTypes = sysDictTypeService.selectDictTypeList(new SysDictType());

        return getDataTable(sysDictTypes);
    }

    @GetMapping("/listDictData")
    @ApiOperation("获取字典项中的值")
    public TableDataInfo listDictData(){
        startPage();
        List<SysDictData> listDict=new ArrayList<>();

        try {
            listDict= (List<SysDictData>) redisTemplate.opsForValue().get(DICT_DATA);
            if (listDict != null) {
                return getDataTable(listDict);
            }
        }catch (Exception e) {
            System.out.println("数据库异常");
        }

        System.out.println("从数据库中获取");

        return getDataTable(listDict);
    }

}
